﻿using System;
using System.Collections.Generic;
using Weather.UA.Model;

namespace Weather.UA
{
    public partial class WeatherUA
    {
        private Country _country = new Country();
        private readonly List<Country> _countries = new List<Country>();

        public Country GetCountryById(int id)
        {
            _country = new Country();
            const string apiUrl = "{0}/{1}/country/?country={2}&lang={3}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, id, Language);
            var document = _loadXDocument(url);
            var country = document.Element("country").Element("country");

            var attributeId = country.Attribute("id");
            var elementName = country.Element("name");
            var elementISO2 = country.Element("ISO2");
            var elementISO3 = country.Element("ISO3");
            var elementSector = country.Element("region");
            var attributeSectorId = elementSector.Attribute("id");

            if (elementName != null && attributeId != null)
            {
                _country.Id = Convert.ToInt32(attributeId.Value);
                _country.Name = elementName.Value;
                if (elementISO2 != null)
                    _country.ISO2 = elementISO2.Value;
                if (elementISO3 != null)
                    _country.ISO3 = elementISO3.Value;
                if (attributeSectorId != null)
                {
                    _country.Sector = new Sector
                    {
                        Id = Convert.ToInt32(attributeSectorId.Value),
                        Name = elementSector.Value
                    };
                }
            }
            return _country;
        }

        public List<Country> GetCountriesByName(string name)
        {
            const string apiUrl = "{0}/{1}/country/?search={2}lang={3}";
            var url = String.Format(apiUrl, ApiDomain, ApiVersion, name, Language);
            var document = _loadXDocument(url);
            var countries = document.Element("country").Descendants("country");
            foreach (var country in countries)
            {
                var attributeId = country.Attribute("id");
                var elementName = country.Element("name");
                var elementISO2 = country.Element("ISO2");
                var elementISO3 = country.Element("ISO3");
                var elementSector = country.Element("region");
                var attributeSectorId = elementSector.Attribute("id");

                if (attributeId != null & elementName != null)
                {
                    var c = new Country();
                    c.Id = Convert.ToInt32(attributeId.Value);
                    c.Name = elementName.Value;
                    if (elementISO2 != null)
                        c.ISO2 = elementISO2.Value;
                    if (elementISO3 != null)
                        c.ISO3 = elementISO3.Value;
                    if (attributeSectorId != null)
                    {
                        c.Sector = new Sector
                        {
                            Id = Convert.ToInt32(attributeSectorId.Value),
                            Name = elementSector.Value
                        };
                    }
                    _countries.Add(c);
                }
            }
            return _countries;
        }
    }
}
